package com.elong.businesstravel.base.baseactivity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat.Action;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.android.support.jhf.handlerui.HandlerPostUI;
import com.android.support.jhf.handlerui.HandlerToastUI;
import com.android.support.jhf.utils.CoordinateConversion;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.elong.businesstravel.R;
import com.elong.businesstravel.base.baidusdk.location.BaiduLocation;
import com.elong.businesstravel.base.baidusdk.map.BaiduMapView;
import com.elong.businesstravel.base.view.TitleNavBarView;

/**
 * 百度地图基类，用来实现每个地图界面都会有的方法
 * 
 * @author lenovo
 * 
 */
abstract public class BaseMapActivity extends BaseActivity {

	protected static class ShowPopupOverlay {
		public String hotelName;
		public String hotelAddress;
//		public GeoPoint p1;
		public LatLng p1;
		public int index;
	}

	protected MapView mMapView = null; // 地图View
	protected BaiduMap mBaiduMap;
	protected BaiduMapView mBaiduMapView;

	// 定位相关
	protected MyLocationData locData = null;

	// protected boolean isRequest = false;// 是否手动触发请求定位
	// protected boolean isFirstLoc = true;// 是否首次定位

	/** true显示弹出框，false不显示弹出框 */
	protected boolean mShowPopupFlag = true;
	/** 显示对话框的数据 */
	protected ShowPopupOverlay mShowPopupOverlay = new ShowPopupOverlay();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	protected void initBaiduMap(MapView mapView) {
		mMapView = (MapView) findViewById(R.id.mapView);
		mBaiduMapView = new BaiduMapView(this, mMapView);
		mBaiduMap = mBaiduMapView.getBaiduMap();
	}

	protected void baiduLocation() {
		HandlerToastUI.getHandlerToastUI(mContext, "正在定位......");
		BaiduLocation.getDefaultBaiduLocation(mApplication)
				.setLocationListener(new BDLocationListener() {

					@Override
					public void onReceivePoi(BDLocation arg0) {

					}

					@Override
					public void onReceiveLocation(BDLocation location) {
						BaiduLocation.getDefaultBaiduLocation(mApplication)
								.stopLocation();

						if (null == mContext
								|| ((Activity) mContext).isFinishing()) {
							return;
						}

						if (null == location || 0 == location.getLatitude()
								|| 0 == location.getLongitude()) {
							HandlerToastUI.getHandlerToastUI(mContext, "定位失败");
							return;
						}

						if (location == null || mMapView == null)
							return;

						mApplication.mBdLocation = location;

						MyLocationData locData = new MyLocationData.Builder()
								.accuracy(location.getRadius())
								// 此处设置开发者获取到的方向信息，顺时针0-360
								.direction(100)
								.latitude(location.getLatitude())
								.longitude(location.getLongitude()).build();
						mBaiduMapView.setLocationOverlayData(locData);
						LatLng ll = new LatLng(location.getLatitude(), location
								.getLongitude());
						MapStatusUpdate u = MapStatusUpdateFactory
								.newLatLng(ll);
						mBaiduMapView.getBaiduMap().animateMapStatus(u);


						HandlerToastUI.getHandlerToastUI(mContext, "定位成功");
						
						getLocationSucceed(location);

					}
				});
		BaiduLocation.getDefaultBaiduLocation(mContext).startLocation();
	}
	
	/**
	 * 定位成功
	 * @param location
	 */
	protected void getLocationSucceed(BDLocation location) {
		
	}

	@Override
	protected void onPause() {
		if (null != mMapView) {
			mMapView.onPause();
		}
		super.onPause();
	}

	@Override
	protected void onResume() {
		if (null != mMapView) {
			mMapView.onResume();
		}
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		if (null != mMapView) {
			mMapView.onDestroy();
		}
		super.onDestroy();
	}

}
